package com.top.mall.main.mine

import android.app.Activity
import android.content.Intent
import android.text.Editable
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.LifecycleOwner
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.OptionsPickerView
import com.bigkoo.pickerview.view.TimePickerView
import com.blankj.utilcode.util.TimeUtils
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadCircleImage
import com.module.lemlin.owner.OwnerViewModeActivity
import com.top.mall.R
import com.top.mall.app.SPSConstance
import com.top.mall.base.BaseViewModel
import com.top.mall.custom.setTransitionStatus
import com.top.mall.entity.Profile
import com.top.mall.entity.UserInfo
import com.top.mall.jetpack.vm.CouponViewModel
import kotlinx.android.synthetic.main.activity_mine_profile.*
import kotlinx.android.synthetic.main.activity_mine_profile.imageViewBack

class MineProfileActivity : OwnerViewModeActivity<BaseViewModel>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MineProfileActivity::class.java)
            activity.startActivity(intent)
        }
    }

    private val mProfile = Profile()

    private val mOptionsGender: OptionsPickerView<String> by lazy {
        OptionsPickerBuilder(this) { options1, option2, options3, v ->
            tvProfileSex.text = if (options1 == 0) "保密" else if (options1 == 1) "男" else "女"
            mProfile.apply { gender = options1 }
        }.build()
    }

    private val mOptionsTime: TimePickerView by lazy {
        TimePickerBuilder(this) { date, view ->
            tvProfileBirthday.text = TimeUtils.date2String(date, "yyyy-MM-dd")
        }
            .setType(
                booleanArrayOf(true, true, true, false, false, false)
            )
            .setLabel(("年"), ("月"), ("日"), ("时"), ("分"), ("秒"))
            .isCenterLabel(false)
            .isCyclic(false)
            .build()
    }

    override fun viewModel(): CouponViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_profile

    override fun stateBar(): StatusBar {
        return StatusBar(view = flMineProfileBar)
    }

    override fun initView() {

        imageViewBack.setOnClickListener { finish() }
        SPSConstance.userInfo?.let {
            fillData(it)
        }
        tvProfileNickname.addTextChangedListener {
            mProfile.apply { nickname = it?.toString() }
        }
        tvProfileSurname.addTextChangedListener {
            mProfile.apply { surname = it?.toString() }
        }
        tvProfileUserName.addTextChangedListener {
            mProfile.apply { name = it?.toString() }
        }
        tvProfileBirthday.addTextChangedListener {
            mProfile.apply { birthday = it?.toString() }
        }
        tvProfileEmail.addTextChangedListener {
            mProfile.apply { email = it?.toString() }
        }

        //提交保存
        tvProfileKeep.setOnClickListener {
            viewModel.userProfile(mProfile)
        }

        //性别选择
        tvProfileSex.setOnClickListener {
            mOptionsGender.setPicker(listOf("保密", "男", "女"))
            mOptionsGender.show()
        }

        //日期选择
        tvProfileBirthday.setOnClickListener {
            mOptionsTime.show()
        }

        //城市选择
        tvProfileCity.setOnClickListener {
            AreaListActivity.open(this)
        }
    }

    override fun initData() {
        viewModel.userIndex()
    }

    override fun initViewMode() {
        viewModel.userIndexLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response?.data?.data?.userInfo
                ?: return@observe
            fillData(data)
        })

        viewModel.userProfileLiveData.observe(this  as LifecycleOwner,{response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS){
                return@observe
            }
            toast(response.data?.msg)
            viewModel.userIndex()
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1 && resultCode == 1) {
            tvProfileCity.text = data?.getStringExtra("result")
            data?.getIntExtra("province", 0)
            data?.getIntExtra("city", 0)
            data?.getIntExtra("district", 0)
            mProfile.apply {
                province = data?.getIntExtra("province", 0)
                city = data?.getIntExtra("city", 0)
                district = data?.getIntExtra("district", 0)
            }
        }
    }

    private fun fillData(userInfo: UserInfo) {

        ivProfileHead.loadCircleImage(
            src = R.mipmap.ic_test_8 //头像
        )
        tvProfileNickname.text = Editable.Factory.getInstance().newEditable(userInfo.nickname) //昵称
        tvProfileId.text = userInfo.id.toString()      //ID
        tvProfileSurname.text = Editable.Factory.getInstance().newEditable(userInfo.surname)//姓
        tvProfileUserName.text = Editable.Factory.getInstance().newEditable(userInfo.user_name)//名
        tvProfileSex.text = when (userInfo.gender) {
            0 -> "保密"
            1 -> "男"
            else -> "女"
        }//性别
        tvProfileBirthday.text = userInfo.birthday //生日
        tvProfileCity.text = userInfo.area     //城市
        tvProfileEmail.text = Editable.Factory.getInstance().newEditable(userInfo.email)    //电子邮箱
        tvProfilePhone.text = Editable.Factory.getInstance().newEditable(userInfo.mobile)    //手机号码

        mProfile.apply {
            surname = userInfo.surname
            name = userInfo.user_name
            birthday = userInfo.birthday
            email = userInfo.email
        }
    }

}